package com.qbw.app.request.impl;

import com.http.okhttp.HttpTask;
import com.qbw.app.Constant;
import com.qbw.app.model.Address;
import com.qbw.app.model.BaseModel;
import com.qbw.app.model.Country;
import com.qbw.app.model.FirmwareUpgrade;
import com.qbw.app.model.Upload;
import com.qbw.app.model.VerifyCode;
import com.qbw.app.model.Version;
import com.qbw.app.model.Weather;
import com.qbw.app.request.ICommonRequest;
import com.qbw.spm.P;

import java.util.HashMap;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class CommonRequest implements ICommonRequest {

    /**
     * 获取国家列表
     */
    public static final String METHOD_COUNTRY_LIST = "common/countries";
    /**
     * 获取验证码
     */
    public static final String METHOD_VERIFY_CODE = "common/sendSms";
    /**
     * 上传文件
     */
    public static final String METHOD_UPLOAD_FILE = "api/uploadFile";
    /**
     * 获取app的最新版本
     */
    public static final String METHOD_CHECK_UPDATE = "common/newVersion";
    /**
     * 校验验证码的有效性
     */
    public static final String METHOD_CHECK_VERIFYCODE = "common/validCode";
    /**
     * 根据经纬度获取物理地址
     */
    public static final String METHOD_GET_ADDRESS = "api/getAddress";
    /**
     * 检查用户是否存在
     */
    public static final String METHOD_USER_EXIST = "common/validPhone";
    /**
     * 注销后停止推送
     */
    public static final String METHOD_STOP_PUSH = "common/stopPush";
    /**
     * 天气
     */
    public static final String METHOD_WEATHER = "common/weather";

    /**
     * 检查当前用户指定车辆固件是否有更新版本 get
     */

    public static final String VEHICE_FIRMWAREVERSION = "deviceVersion/checkFirmwareVersion";


    /**
     * 用户车辆固件升级完成反馈 post
     */
    public static final String VEHICE_UPDATEFIRMWAREVERSION = "deviceVersion/updateFirmwareVersion";


    //
    @Override
    public HttpTask getCountryList(HttpTask.CallBack callBack) {
        return HttpTask.create(METHOD_COUNTRY_LIST,
                               null,
                               Country.Response.class,
                               null,
                               new HttpTask.SimpleFlowCallBack() {
                                   @Override
                                   public void onSuccess(HttpTask httpTask,
                                                         Object entity,
                                                         String modelStr) {
                                       super.onSuccess(httpTask, entity, modelStr);
                                       P.putString(Constant.KEY_ALL_COUNTRYS, modelStr);
                                   }
                               },
                               callBack,
                               null).get();
    }

    @Override
    public HttpTask getVerifyCode(String telCode,
                                  String phone,
                                  int type,
                                  HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        params.put("type", type);
        return HttpTask.create(METHOD_VERIFY_CODE, params, VerifyCode.class, callBack).post();
    }

    @Override
    public HttpTask uploadFile(String file, HttpTask.CallBack callBack) {
        HashMap<String, String> files = new HashMap<>();
        files.put("file", file);
        return HttpTask.create(METHOD_UPLOAD_FILE, null, Upload.class, callBack).upload(files);
    }

    @Override
    public HttpTask checkUpdate(HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("appOs", 1);
        return HttpTask.create(METHOD_CHECK_UPDATE, params, Version.Response.class, callBack).get();
    }

    @Override
    public HttpTask checkVerifyCode(String telCode,
                                    String phone,
                                    String code,
                                    HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        params.put("code", code);
        return HttpTask.create(METHOD_CHECK_VERIFYCODE, params, BaseModel.class, callBack).post();
    }

    @Override
    public HttpTask getAddress(String lat, String lon, HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("lat", lat);
        params.put("lon", lon);
        return HttpTask.create(METHOD_GET_ADDRESS, params, Address.class, callBack).get();
    }

    @Override
    public HttpTask checkUserExist(String telCode, String phone, HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("telCode", telCode);
        params.put("phone", phone);
        return HttpTask.create(METHOD_USER_EXIST, params, BaseModel.class, callBack).post();
    }

    @Override
    public HttpTask stopPush(String deviceToken, HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("deviceToken", deviceToken);
        return HttpTask.create(METHOD_STOP_PUSH, params, BaseModel.class, callBack).get();
    }


    @Override
    public HttpTask checkFirmwareVersion(String deviceCode,String type, HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("deviceCode", deviceCode);
        params.put("type", type);
        return HttpTask.create(VEHICE_FIRMWAREVERSION, params, FirmwareUpgrade.Response.class, callBack).get();
    }

    @Override
    public HttpTask checkFirmwareVersionFeedback(String deviceCode, String type, String updateState,HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("deviceCode", deviceCode);
        params.put("type", type);
        params.put("updateState", updateState);
//        params.put("versionNo", "GT508_N1M_V0.02_OTA_0429.bin");

        return HttpTask.create(VEHICE_UPDATEFIRMWAREVERSION, params, BaseModel.class, callBack).post();
    }

    @Override
    public HttpTask getWeather(String lat,
                               String lon,
                               String cityName,
                               HttpTask.CallBack callBack) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("lat", lat);
        params.put("lon", lon);
        params.put("cityName", cityName);
        return HttpTask.create(METHOD_WEATHER, params, Weather.Response.class, callBack).get();
    }
}
